<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <title>小样订单</title>
    <!--{include file='comm/comm_region.tpl'}-->

    <!--{include file='comm/comm_cal.tpl'}-->
    <script language="JavaScript" src="/framework/jquery/jquery.jqprint-0.3.js?v=1" type="text/javascript"></script>
</head>

<body>

<div class="panel panel-default">

    <div class="comm_right">
        <div class="comm_crub">
            <span class="now_crub">小样订单</span>
            <input type="button" class="btn btn-default" style="float: right; margin-top: 8px" onclick="print();" value="打印" />
        </div>

        <div class="comm_content">

            <div class="panel-body">

                <div class="form-horizontal" style="position:relative">
                    <div class="form-group" >
                        <label class="col-sm-2 control-label">
                            客户信息：
                        </label>
                        <div class="col-sm-3">
                            <input type="hidden" id="tempCode" value="<!--{$tempInfo->tempCode|default:''}-->">
                            <input type="hidden" id="companyId" value="<!--{$tempInfo->companyId|default:''}-->">
                            <input id="companyName" type="text" class="form-control" readonly="readonly" value="<!--{$tempInfo->company_name|default:''}-->">
                        </div>
                        <div class="col-sm-3">
                            <button type="button" class="btn btn-primary" onclick="searchCompany(1);">查询</button>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-sm-2 control-label">
                            项目名称：
                        </label>
                        <div class="col-sm-3">
                            <input id="tempName" class="form-control" value="<!--{$tempInfo->tempName|default:''}-->"/>
                        </div>
                        <label class="col-sm-2 control-label">
                            施工面积：
                        </label>
                        <div class="col-sm-3">
                            <input id="area" class="form-control" value="<!--{$tempInfo->area|default:''}-->"/>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-sm-2 control-label">
                            涂装体系：
                        </label>
                        <div class="col-sm-3">
                            <input id="categoryName" class="form-control" value="<!--{$tempInfo->categoryName|default:''}-->"/>
                        </div>
                        <label class="col-sm-2 control-label">
                            物流方式：
                        </label>
                        <div class="col-sm-3">
                            <select class="form-control" id="expressId">
                                <option value="">请选择</option>
                                <!--{foreach from=$send_array item=send}-->
                                <!--{if $tempInfo && $tempInfo->expressId == $send['sendTypeID']}-->
                                <option value="<!--{$send['sendTypeID']}-->" selected><!--{$send['sendTypeName']}--></option>
                                <!--{else}-->
                                <option value="<!--{$send['sendTypeID']}-->"><!--{$send['sendTypeName']}--></option>
                                <!--{/if}-->
                                <!--{/foreach}-->
                            </select>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-sm-2 control-label">
                            收货人：
                        </label>
                        <div class="col-sm-3">
                            <input id="receiver" class="form-control" value="<!--{$tempInfo->receiver|default:''}-->"/>
                        </div>
                        <label class="col-sm-2 control-label">
                            收货人电话：
                        </label>
                        <div class="col-sm-3">
                            <input id="receivMobile" class="form-control" value="<!--{$tempInfo->receivMobile|default:''}-->"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">
                            收货地址：
                        </label>
                        <div class="col-sm-10">
                            <select class="form-control fl" id="province" style="width: 100px;margin-right: 5px;">
                                <option value="">请选择...</option>
                            </select>
                            <select class="form-control fl" id="city" style="width: 100px;margin-right: 5px;">
                                <option value="">请选择...</option>
                            </select>
                            <select class="form-control fl" id="county" style="width: 100px;margin-right: 5px;">
                                <option value="">请选择...</option>
                            </select>
                            <input id="address" class="form-control fl w300"value="<!--{$tempInfo->address|default:''}-->" placeholder="详细地址"/>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-sm-2 control-label">
                            制作内容及要求：
                        </label>
                        <div class="col-sm-3">
                            <textarea id="request" class="form-control" rows="3"><!--{$tempInfo->request|default:''}--></textarea>
                        </div>
                        <label class="col-sm-2 control-label">
                            要求完成时间：
                        </label>
                        <div class="col-sm-3">
                            <input id="requestDate" class="form-control" value="<!--{$tempInfo->requestDate|default:''}-->" readonly="readonly" />
                        </div>
                    </div>
                    <!--{if $tempInfo->expressNum}-->
                    <div class="form-group">
                        <label class="col-sm-2 control-label">
                            物流单号：
                        </label>
                        <div class="col-sm-3">
                            <input id="expressNum" class="form-control" value="<!--{$tempInfo->expressNum|default:''}-->"/>
                        </div>
                    </div>
                    <!--{/if}-->
                    <div class="form-group">
                        <label class="col-sm-2 control-label">
                            备注：
                        </label>
                        <div class="col-sm-10">
                            <textarea id="remark" class="form-control w300" rows="3"><!--{$tempInfo->remark|default:''}--></textarea>
                        </div>
                    </div>

                    <div style="text-align: right; margin-bottom: 5px">
                        <input class="btn btn-primary btn-sm" type="button" onclick="addGoods()" value="添加"/>
                    </div>
                    <table class="table table-bordered table-hover">
                        <thead>
                        <tr>
                            <th width="12%" style="text-align:center;">产品系列</th>
                            <th width="14%" style="text-align:center;">颜色</th>
                            <th width="9%" style="text-align:center;">对色标准</th>
                            <th width="9%" style="text-align:center;">制作内容</th>
                            <th width="10%" style="text-align:center;">规格</th>
                            <th width="10%" style="text-align:center;">数量</th>
                            <th width="10%" style="text-align:center;">效果</th>
                            <th width="12%" style="text-align:center;">备注</th>
                            <th width="5%" style="text-align:center;">操作</th>
                        </tr>
                        </thead>
                        <tbody id="goodsList">
                        <!--{if $tempInfo}-->
                        <!--{foreach from=$tempInfo->dtl item=goods}-->
                            <tr>
                                <td>
                                    <select class="form-control" name="category" disabled="disabled">
                                        <option value="<!--{$goods->categoryId}-->"><!--{$goods->categoryName}--></option>
                                    </select>
                                </td>
                                <td><input class="form-control" name="color" value="<!--{$goods->color}-->"/></td>
                                <td><input class="form-control" name="colorDis" value="<!--{$goods->colorDis}-->"/></td>
                                <td>
                                    <select class="form-control" name="make" disabled="disabled">
                                        <option><!--{$goods->make}--></option>
                                    </select>
                                </td>
                                <td><input class="form-control" name="spec" placeholder="长x宽(厘米)" value="<!--{$goods->spec}-->"/></td>
                                <td><input class="form-control" name="amount" value="<!--{$goods->amount}-->"/></td>
                                <td><input class="form-control" name="effect" value="<!--{$goods->effect}-->"/></td>
                                <td><input class="form-control" name="remark" value="<!--{$goods->remark}-->"/></td>
                                <td style="text-align: center; line-height: 30px"><a>刪除</a></td>
                            </tr>
                        <!--{/foreach}-->
                        <!--{else}-->
                            <tr>
                                <td><select class="form-control" name="category"></select></td>
                                <td><input class="form-control" name="color"/></td>
                                <td><input class="form-control" name="colorDis"/></td>
                                <td><select class="form-control" name="make"></select></td>
                                <td><input class="form-control" name="spec" placeholder="长x宽(厘米)"/></td>
                                <td><input class="form-control" name="amount"/></td>
                                <td><input class="form-control" name="effect"/></td>
                                <td><input class="form-control" name="remark"/></td>
                                <td style="text-align: center; line-height: 30px"><a>刪除</a></td>
                            </tr>
                        <!--{/if}-->
                        </tbody>
                    </table>
                    <div style="font-size: 14px">
                        <span name="currDate" style="float: right">日期：<!--{$currDate}--></span>
                    </div>
                    <!--{if $tempInfo->tempCode}-->
                    <div style="float: right; clear: both; padding-top: 10px">
                        <img style="width: 200px;height: 200px;border: 1px solid #ccc" src="/framework/qrcode/data.php?text=<!--{$tempInfo->tempCode}-->" alt=""/>
                    </div>

                    <!--{if $tempInfo->annex}-->
                    <!--{foreach from=$tempInfo->annex key=categoryName item=alist}-->
                    <div style="padding: 5px">
                        <div style="float: left; width: 100px"><!--{$categoryName}-->:</div>
                        <!--{foreach from=$alist item=annex}-->
                        <img src="<!--{$annex->path}-->" style="height: 200px; max-width: 400px; margin-left: 10px">
                        <!--{/foreach}-->
                    </div>
                    <!--{/foreach}-->
                    <!--{/if}-->
                    <!--{/if}-->
                </div>
            </div>
        </div>
        <div class="bottom_fixbox" style="margin-left: 0px;width: 100%;left: 0px; height: 46px;">
            <div class="tac" style="background: #dddddd;">
                <!--{if empty($tempInfo) || $tempInfo->tempStatus == 0 }-->
                <input type="button" class="btn btn-primary" onclick="saveTemp()" value="保存" />&nbsp;&nbsp;&nbsp;
                <!--{elseif $tempInfo->tempStatus == 3}-->
                <input type="button" class="btn btn-primary" onclick="sendTemp()" value="发货" />&nbsp;&nbsp;&nbsp;
                <!--{/if}-->
                <input type="button" class="btn btn-default" onclick="E.closeWindows();" value="关闭" />
            </div>
        </div>
    </div>

</div>
<script type="text/javascript">
    
    function print() {
        $('.comm_content').jqprint({
            debug: false, //如果是true则可以显示iframe查看效果（iframe默认高和宽都很小，可以再源码中调大），默认是false
            importCSS: true, //true表示引进原来的页面的css，默认是true。（如果是true，先会找$("link[media=print]")，若没有会去找$("link")中的css文件）
            printContainer: true, //表示如果原来选择的对象必须被纳入打印（注意：设置为false可能会打破你的CSS规则）。
            operaSupport: false//表示如果插件也必须支持歌opera浏览器，在这种情况下，它提供了建立一个临时的打印选项卡。默认是true
        });
    }
    
    //显示省市区
    E.region.init({
        p: 'province',
        c: 'city',
        a: 'county',
        pid: <!--{$tempInfo->province|default:0}-->,
        cid: <!--{$tempInfo->city|default:0}-->,
        ccid: <!--{$tempInfo->county|default:0}-->
    });

    var categoryData = <!--{$category|default:'{}'}-->;
    var makeData = <!--{$make|default:'{}'}-->;
    $(function () {
        <!--{if $tempInfo && $tempInfo->tempStatus !=0}-->
        $('.comm_content').find('input,select,textarea,button,a').attr('disabled', 'disabled');
        $('#goodsList a').hide();
        <!--{/if}-->

        $('#goodsList').on('click','a', function () {
            if($('#goodsList tr').length==1){
                E.alert('至少包含一种材料');
                return;
            }
            $(this).parents('tr').remove();
        });

        //绑定分类
        $('#goodsList select[name=category]:not(:disabled)').each(function () {
            bindCategory($(this));
        });

        //绑定分类
        $('#goodsList select[name=make]:not(:disabled)').each(function () {
            bindMake($(this));
        });

        //切换制作内容
        $('#goodsList').on('change','select[name=make]', function () {
            var _tr = $(this).parents('tr'),
                val = $(this).find("option:selected").text();
            if(val == '物料') {
                _tr.find('input[name=spec]').val('').attr('placeholder','平方');
            }
            if(val == '样板') {
                _tr.find('input[name=spec]').val('').attr('placeholder','长x宽(厘米)');
            }
        });

        $('#requestDate').datepicker();
    });

    //绑定分类数据
    function bindCategory (obj, selectId) {
        var html = ''; selectId = selectId || '';
        $.each(categoryData, function (i, v) {
            html += '<option value="'+ v.bigCategoryID +'" '+ (v.bigCategoryID==selectId?'selected':'') +'>'+ v.name +'</option>';
        });
        obj.append(html);
    }
    //绑定制作内容
    function bindMake (obj, selectId) {
        var html = ''; selectId = selectId || '';
        $.each(makeData, function (i, v) {
            html += '<option value="'+ v +'" '+ (v==selectId?'selected':'') +'>'+ v +'</option>';
        });
        obj.append(html);
    }


    //添加商品行
    function addGoods () {
        var html = '', _tr = $('<tr></tr>');
        html += '   <td><select class="form-control" name="category"></select></td>';
        html += '   <td><input class="form-control" name="color"/></td>';
        html += '   <td><input class="form-control" name="colorDis"/></td>';
        html += '   <td><select class="form-control" name="make"></select></td>';
        html += '   <td><input class="form-control" name="spec" placeholder="长x宽(厘米)"/></td>';
        html += '   <td><input class="form-control" name="amount"/></td>';
        html += '   <td><input class="form-control" name="effect"/></td>';
        html += '   <td><input class="form-control" name="remark"/></td>';
        html += '   <td style="text-align: center; line-height: 30px"><a>刪除</a></td>';
        _tr.append(html);
        $('#goodsList').append(_tr);
        bindCategory(_tr.find('select[name=category]'));
        bindMake(_tr.find('select[name=make]'));
    }

    //查询客户
    function searchCompany (page) {
        var company_name="";
        if($("#searchCompanyName").val()!=undefined){
            company_name = $("#searchCompanyName").val();
        }

        E.loadding.open('正在查询，请稍候...');

        E.ajax_post({
            url:  "/invokingAjax-backend/company/company.ajax?operFlg=1&company_name="+company_name,
            data: {
                sortname: 'company_id',
                sortorder: 'ASC',
                rp: 10,
                page: page,
                list_type: 'general',
                show_type: 'company',
                way_type:'searchCompany'
            },
            call: function( o ) {

                E.loadding.close();
                if (o.data) {

                    var html_str = '<table><tr><td style="width: 80px;">公司名称：</td><td><input type="text" class="form-control w300" id="searchCompanyName" name="searchCompanyName" value='+o.company_name+'></span></td><td><input type="button" value="查询" id="searchBtn" class="btn btn-primary" onclick="searchCompany(1);" /></td></tr></table>';
                    html_str += '<table class="s">';
                    html_str += '<tbody>';

                    html_str += '<tr>';
                    html_str += '<th width="80">操作</th>';
                    html_str += '<th width="*">公司名称</th>';
                    html_str += '<th width="100">联系人</th>';
                    html_str += '<th width="100">联系方式</th>';
                    html_str += '<th width="180">部门</th>';
                    html_str += '<th width="100">销售员</th>';

                    html_str += '</tr>';
                    $.each(o.data, function(k, v) {

                        if (v.mobile) {
                            var contact_way = v.mobile;
                        } else {
                            var contact_way = v.phone;
                        }

                        html_str += '<tr>';
                        html_str += '<td class="tac"><a href="javascript:void(0)" class="company_line" val0='+v.company_id+' val1='+v.company_name+' val2='+ v.connect_name+' val3='+ contact_way +' onclick="changeCompany('+v.company_id+');">选择</a></td>';
                        html_str += '<td class="tac" style="text-align:left">' + v.company_name + '</td>';
                        html_str += '<td class="tac">' + v.connect_name + '</td>';
                        html_str += '<td class="tac">' + contact_way + '</td>';
                        html_str += '<td class="tac">' + v.departmentName + '</td>';
                        html_str += '<td class="tac">' + v.userName + '</td>';
                        html_str += '</tr>';

                    });
                    html_str += '</tbody>';
                    html_str += '</table>';

                    if (o.paging) {
                        html_str += o.paging;
                    }

                    E.popup.open({
                        content: html_str,
                        title: '大客户信息列表',
                        css: 'width: 800px;'
                    });

                }else{
                    var html_str = '<table><tr><td style="width: 80px;">公司名称：</td><td><input type="text" class="form-control w300" id="searchCompanyName" name="searchCompanyName" value='+o.company_name+'></span></td><td><input type="button" value="查询" id="searchBtn" class="btn btn-primary" onclick="searchCompany(1);" /></td></tr></table>';
                    html_str += '<table class="s">';
                    html_str += '<tbody>';

                    html_str += '<tr>';
                    html_str += '<th width="80">操作</th>';
                    html_str += '<th width="*">公司名称</th>';
                    html_str += '<th width="100">联系人</th>';
                    html_str += '<th width="100">联系方式</th>';
                    html_str += '<th width="180">部门</th>';
                    html_str += '<th width="100">销售员</th>';

                    html_str += '</tr>';
                    html_str += '</tbody>';
                    html_str += '</table>';
                    E.popup.open({
                        content: html_str,
                        title: '大客户信息列表',
                        css: 'width: 800px;'
                    });
                }
            }
        });
    }

    //选择客户
    function changeCompany (searchCompanyID){
        E.ajax_post({
            url: '/invokingAjax-backend/company/company.ajax?operFlg=100&act=search',
            data: {
                company_id: searchCompanyID
            },
            call: function( o ) {
                if (o.code == 200) {
                    if ( o.data ) {
                        $.each( o.data, function( k, v ) {
                            $('#companyId').val(v.company_id);
                            $('#companyName').val(v.company_name);
                        });
                    }
                } else {
                    E.alert( o.message);
                }
                E.popup.close();
            }
        });
    }

    //保存数据
    function saveTemp() {
        var tempCode = $('#tempCode').val(),
            companyId = $('#companyId').val(),
            companyName = $('#companyName').val(),
            tempName = $('#tempName').val(),
            area = $('#area').val(),
            categoryName = $('#categoryName').val(),
            expressId = $('#expressId').val(),
            receiver = $('#receiver').val(),
            receivMobile = $('#receivMobile').val(),
            province = $('#province').val(),
            provinceName = $('#province').find('option:selected').text(),
            city = $('#city').val(),
            cityName = $('#city').find('option:selected').text(),
            county = $('#county').val(),
            countyName = $('#county').find('option:selected').text(),
            address = $('#address').val(),
            request = $('#request').val(),
            requestDate = $('#requestDate').val(),
            remark = $('#remark').val();

        var error_msg = '';
        if (E.isEmpty(companyId))
            error_msg += '请选择客户<br/>';
        if (E.isEmpty(tempName))
            error_msg += '请填写项目名称<br/>';
        if (E.isEmpty(area))
            error_msg += '请填写施工面积<br/>';
        if (E.isEmpty(categoryName))
            error_msg += '请填写涂装体系<br/>';
        if (E.isEmpty(expressId))
            error_msg += '请选择物流方式<br/>';
        if (E.isEmpty(receiver))
            error_msg += '请填写收货人<br/>';
        if (E.isEmpty(receivMobile))
            error_msg += '请填写收货人手机<br/>';
        if (E.isEmpty(province)||E.isEmpty(city)||E.isEmpty(county)||E.isEmpty(address))
            error_msg += '请填写完整的地址信息<br/>';
        if (E.isEmpty(request))
            error_msg += '请填写制作内容及要求<br/>';
        if (E.isEmpty(requestDate))
            error_msg += '请选择完成时间<br/>';

        if (error_msg != '') {
            E.alert(error_msg);
            return;
        }

        var goodsData = [], goodsTr = $('#goodsList tr');
        if (goodsTr.length == 0) {
            error_msg += '请选择材料<br/>';
        } else {
            $.each(goodsTr, function (i, v) {
                var goodsCategoryId = $(v).find('select[name=category]').val(),
                    goodsCategoryName = $(v).find('select[name=category]').find("option:selected").text(),
                    color = $(v).find('input[name=color]').val(),
                    colorDis = $(v).find('input[name=colorDis]').val(),
                    make = $(v).find('select[name=make]').val(),
                    spec = $(v).find('input[name=spec]').val(),
                    amount = $(v).find('input[name=amount]').val(),
                    effect = $(v).find('input[name=effect]').val(),
                    remark = $(v).find('input[name=remark]').val();

                if (E.isEmpty(goodsCategoryId)) {
                    error_msg += '请选择您要添加的产品<br/>';
                }
                if (E.isEmpty(color) || E.isEmpty(colorDis) || E.isEmpty(make) || E.isEmpty(spec) || E.isEmpty(amount)|| E.isEmpty(effect)) {
                    error_msg += '请补全产品信息<br/>';
                }

                goodsData.push({
                    goodsCategoryId: goodsCategoryId,
                    goodsCategoryName: goodsCategoryName,
                    color: color,
                    colorDis: colorDis,
                    make: make,
                    spec: spec,
                    amount: amount,
                    effect: effect,
                    remark: remark
                });
            });
        }

        if (error_msg != '') {
            E.alert(error_msg);
            return;
        }

        E.ajax_post({
            url: '/invokingAjax-backend/bill/billTemp.ajax?operFlg=4',
            data: {
                tempCode: tempCode,
                companyId: companyId,
                companyName: companyName,
                tempName: tempName,
                area: area,
                categoryName: categoryName,
                expressId: expressId,
                receiver: receiver,
                receivMobile: receivMobile,
                province: province,
                provinceName: provinceName,
                city: city,
                cityName: cityName,
                county: county,
                countyName: countyName,
                address: address,
                request: request,
                requestDate: requestDate,
                remark: remark,
                validDate: $('span[name=validDate]').html(),
                dtl: goodsData
            },
            call: function (o) {
                if (o.code == 200) {
                    E.alert(o.message, 2, function () {
                        E.opener('temp.load');
                        E.closeWindows();
                    });
                } else {
                    E.alert(o.message)
                }
            }
        });
    }

    function sendTemp() {

        var html = '<div class="form-group" style="line-height: 35px"><label class="col-sm-3 control-label">物流单号：</label><div class="col-sm-8"><input class="form-control" name="expressNum"/></div></div>';
        E.popup.open({
            content: html,
            title: '发货',
            css: 'width: 400px;',
            btnName:'确认',
            isCancelBtn:false,
            btnCall: 'send()'
        });
    }
    function send() {
        var expressNum = $('input[name=expressNum]').val(),
            tempCode = $('#tempCode').val();
        if($.trim(expressNum)==''){
            E.alert('请输入物流单号');return
        }
        E.ajax_post({
            url: '/invokingAjax-backend/bill/billTemp.ajax?operFlg=5',
            data: { code: tempCode, expressNum: expressNum },
            call: function (o) {
                if (o.code == 200) {
                    E.alert(o.message, 2, function () {
                        E.opener('temp.load');
                        E.closeWindows();
                    });
                } else {
                    E.alert(o.message)
                }
            }
        });
    }
</script>

</body>
</html>
